<?php

// dependancies:
// cf_db : CF_Pdo_Common :  get_array_query_full()


class CF_LookupResolver {


/**
*
* $static_spec: array('<column1>'=><lookup_list 1>, ...
* each lookup_list:  array(0=>array('keyvalue'=><key>,'showvalue'=><value>, ...), ...)
* Just making this public so we can manipulate it directly.
*
*/
public $static_spec = array();
/**
*
* $query_spec: array('<column1>'=>array('query'=><query_string>, 'params'=>array('<param column1>,<param column2>...), 'order'=><order_array>), ...)
* <order_array>:  array('<order column1>'=>{'ASC', 'DESC'}, ...)
* <query_string>: string for query, should include outputs as 'keyvalue' and 'showvalue', and end with 'WHERE 1'
* each lookup_list:  array(0=>array('keyvalue'=><key>,'showvalue'=><value>, ...), ...)
* Just making this public so we can manipulate it directly.
*
*/
var $query_spec = array();


/**
*
* lookup_functions - have form: lookup_<column>($base_array)
* Always except parameter for base_array, which accepts whatever lookup list has been created through static or query methods
*
*/


/**
* $set:  array('<column1>'=><value1>,'<column2>'=<value2>, ...)
* $ar_columns:  array('column1','column2',...)
*
* return:  array of lookup lists array('<column>'=><lookup_list>, ...)
* each lookup_list:  array(0=>array('keyvalue'=><key>,'showvalue'=><value>, ...), ...)
* 
*/
	function GetLists($ar_columns, $set = null){
		$ar_lookup_lists = array();
		foreach($ar_columns as $column){
			$lookup_list = $this->GetList($column, $set);
			$ar_lookup_lists[$column] = $lookup_list;
		}
		return $ar_lookup_lists;
	}

	function GetList($column, $set = null){
		$lookup_list = array();
	// in $static_spec?  get that
		if(!empty($this->static_spec[$column])){
			$lookup_list = $this->static_spec[$column];
		}
		// in query_spec?  get that and append to end
		if(!empty($this->query_spec[$column])){
			$query = $this->query_spec[$column]['query'];
			$params = $this->query_spec[$column]['params'];
			$orderby = $this->query_spec[$column]['orderby'];
			// set each query parameter as appropriate, if it is supplied in the $set
			if(is_array($params)){
			$queryparams = array();
			//print "LOOKHERE";  //DEBUG
			//print_r($params);
			//exit;
			foreach($params as $param){
				if(!empty($set[$param])){
				//print 'HERE now,set='; // DEBUG
				//print_r($set);
					$queryparams[] = array('var'=>$param,'val'=>$set[$param], 'op'=>'=');
				}
			}
			}
			//print 'queryparams';
			//print_r($queryparams); // DEBUG
			
			$new_list = get_array_query_full($query,$queryparams,$orderby);
			$lookup_list = array_merge($lookup_list,$new_list);
			
		}
		// function exists?  run what we have through it.
		$lookupfunction = 'lookup_' . $column;
		if(method_exists($this,$lookupfunction)){
			$lookup_list = $this->$lookupfunction($lookup_list);
		}
		// return
		return $lookup_list;
	}

	

}
?>